#include <cstdlib>
#include <iostream>
#include <string>
#include "Ccharacter.h"
#include "Enemy.h"
#include "fight.h"
//#include <libc.h>

using namespace std;

// Function to test if player lost
//  char1: Character class object. Contains everything (att,def,health,etc)
// @return: 1 if lose, 0 if no lose
int lose(Ccharacter *char1) {
	if( char1->health <= 0 ) {
		cout << endl << "You lost the fight...failure" << endl;
		cout << "You lose half your gold!!!" << endl;
		char1->money = char1->money/2;
		//sleep(2);
		return 1;
	}
	return 0;
}


// Function to test if player won
//  char1: Character class object. Contains everything (att,def,health,etc)
// @return: 1 if win, 0 if no win
int win(Enemy *enemy, Ccharacter *char1) {
	if( enemy->health <= 0 ) {
		cout << endl << "You won the fight! What a hero!" << endl;
		cout << "You get " << enemy->reward << " pieces of gold" << endl;
		char1->money = char1->money + enemy->reward;
		//sleep(2);
		return 1;
	}
	return 0;
}

// You attack
//  char1: Character class object. Contains everything (att,def,health,etc)
//  enemy: Enemy class object. Contains all its stats
int attack(Ccharacter *char1, Enemy *enemy, int att) {
	int crit = rand() % 15; // for critical hits

	cout << endl << "You attack!" << endl;
	int number = 20 + rand() % 6;
	int damage = (((att*2 - enemy->fight_def) + number) * 2) / 3;
	if (damage <= 0) {
		damage = 1 + rand() % 5;
	}
	//sleep(1);

	if (crit == 0) { // crit
		cout << "You strike with a critical hit!" << endl;
		damage = damage * 3/2;
		cout << "You damage " << enemy->name << " with " << damage << " points of AMAZING damage!" << endl;
	} else {		// normal attack
	cout << "You deal " << damage << " points of damage!" << endl;
	}
	//sleep(1);
	enemy->health = enemy->health - damage;
	// check health, see if win
	if( 1 == win(enemy, char1) ) {
		return 1;
	}
	return 0;
}

// You defend
//  char1: Character class object. Contains everything (att,def,health,etc)
//  enemy: Enemy class object. Contains all its stats
int defense(Ccharacter *char1, Enemy *enemy, int def) {
	int crit = rand() % 15; // for critical hits

	cout << enemy->name << " attacks!" << endl;
	int number = 20 + rand() % 6;
	int damage = (((enemy->fight_att*2 - def) + number) * 2) / 3;
	if (damage <= 0) {
		damage = 1 + rand() % 6;
	}
	//sleep(1);

	if (crit == 0) { // crit
		cout << enemy->name << " makes contact with a critical!" << endl;
		damage = damage * 3/2;
		cout << enemy->name << " deals " << damage << " of damage! Devastating blow!";
	} else {		// normal attack
		cout << "The " << enemy->name << " deals " << damage << " points of damage!" << endl;
	}
	//sleep(1);
	char1->health = char1->health - damage;

	// check health, see if lose
	if( char1->health < 0 ) {
		char1->health = 0;
	}
	if( 1 == lose(char1) ) {
		return 1;
	}
	return 0;
}


// START
// Fight simulation
void fight (Ccharacter *char1, Enemy enemy) {
	cout << "====================" << endl;
	cout << char1->name << " VERSUS " << enemy.name << endl;
	cout << "====================" << endl;
	cout << "    BEGIN FIGHT!" << endl;
	cout << "====================" << endl;

	string action;
	int tmp_att;
	int tmp_def;
	int stat;


	while(1) {
		cout << "What will you do?: offense, defense, normal, run" << endl;
		getline( cin, action );
		cout << endl << "______________________" << endl;

		enemy.fight_mode();

		// change back to normal att/def stats
		tmp_att = char1->att;
		tmp_def = char1->def;
		stat=0;

		// attack offensively (more damage dealt and recieved)
		if (action == "offense") {
			cout << "You prepare for an aggresive attack!" << endl << endl;
			tmp_att = char1->att*13/10;
			tmp_def = char1->def*7/10;
			stat=1;
		}

		// attack defensively (less damage dealt and recieved)
		if (action == "defense") {
			cout << "You steady yourself for a defensive round" << endl << endl;
			tmp_att = char1->att*7/10;
			tmp_def = char1->def*13/10;
			stat=1;
		}

		// normal attack (no change)
		if (action == "normal") {
			cout << "You attack with an overall normal technique" << endl << endl;
			stat=1;
		}

		// run away (stat changes to 2 to avoid if-statement)
		if (action == "run") {
			stat=2;
			int random = rand() % 2;
			//sleep(1);
			if (random == 1) {
				cout << endl << "You and your blazing feet...you got away!" << endl;
				//sleep(2);
				return;

			} else {
				cout << endl << "You trip over a rock! What a fail! You get attacked" << endl;
				//sleep(2);
				// you get attacked
				if ( 1 == defense( char1, &enemy, tmp_def )) {
					return;
				}
			}
		}

		if (stat==1){
			// speed. if faster, att first. if slower, defend first
			if (char1->spd >= enemy.spd ) {
				if ( 1 == attack( char1, &enemy, tmp_att )) {
					return;
				}
				if ( 1 == defense( char1, &enemy, tmp_def )) {
					return;
				}
			} else {
				if ( 1 == defense( char1, &enemy, tmp_def )) {
					return;
				}
				if ( 1 == attack( char1, &enemy, tmp_att )) {
					return;
				}
			}
			// Unknown option...
		} else if ( stat == 0 ){
			cout << "Unknown Option" << endl;
		}

		// display yo' stats
		cout << "-------------------" << endl;
		cout << char1->name << endl;
		cout << "Health: " << char1->health << endl;
		cout << "------- vs. -------" << endl;
		cout << enemy.name << endl;
		cout << "Health: " << enemy.health << endl;
		cout << "-------------------" << endl;
	}
}
